home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc / Sample Code / CALib & You… / Source / CASample / CAS_Win.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-07  |  24.1 KB  |  920 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        CAS_Win.c
  3.  
  4.     Contains:    window-handling routines.
  5.  
  6.     Written by:    David H Nelson
  7.  
  8.     Copyright © 1993-1995 ComponentWorks, All rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.                  05/09/95    SJF        Tweaks for WWDC Demo
  13.         --------------------------------------
  14.  
  15.                  05/08/95    SJF        Remove the DnD handlers before we close the window.
  16.                                      Remove the DnD handler installation section, as it's
  17.                                     now done in CAS_Doc.c
  18.         --------------------------------------
  19.  
  20.                  04/19/95    RB        Modified scroll procedure to work with new
  21.                                      CALib Layout API.
  22.         --------------------------------------
  23.                  03/31/95    RB        Added code to Win_Close() to select the
  24.                                      next document window
  25.         --------------------------------------
  26.                  03/29/95    RB        Adding scrolling support for embedded parts
  27.         --------------------------------------
  28.                  03/27/95    DAS        converted "theDoc->changed" reference into API call
  29.         --------------------------------------
  30.                  01/23/95    SJF        Add CALib support for window allocation
  31.         --------------------------------------
  32.                  01/18/95    DHN        Global function and variable name changes to suggest
  33.                                      OOP design. Split app.c file into app.c, win.c, doc.c,
  34.                                      item.c, and util.c.
  35.         --------------------------------------
  36.                  01/17/95    DAS        changed all FrontWindow() calls to
  37.                                      App_GetFrontDocWindow() to account for
  38.                                      floating windows. Added Win_IsToolWindow().
  39.                   1/16/95    DHN        Massive improvements including, multiple window
  40.                                       support, new window offsetting and naming, pref
  41.                                     file support, file saving/reading, cursor tracking,
  42.                                     PowerPC support, window update/activate in dialog
  43.                                     filter, item drawing/selection/cut/copy (not paste),
  44.                                     drag & drop (no drop yet), window scrolling, get
  45.                                     info dialog, Select all menu item, Tools menu.
  46.                     through
  47.                  12/23/94    DHN        Created CASample from existing Light Software
  48.                                      application framework.
  49.         --------------------------------------
  50.                   1/24/93    DHN        Added code to ignore the enter key. Fixed page 
  51.                                       flipping animation by using current GrafPort in 
  52.                                       local coords rather than new GrafPort with global
  53.                                       coords. Fixed findAgain to actually do something.
  54.                  11/20/93    DHN        Created.
  55. */
  56.  
  57.  
  58. #ifdef USE_CALIB
  59. #include "CALib.h"
  60. #include "CAS_CAUtil.h"
  61. #endif
  62.  
  63. #include "CAS_Globals.h"
  64. #include "CAS_Misc.h"
  65. #include "CAS_Win.h"
  66. #include "CAS_Doc.h"
  67. #include "CAS_App.h"
  68. #include "CAS_ToolPalette.h"
  69. #include "CAS_Event.h"
  70. #include "CAS_Dialog.h"
  71.  
  72. //---------------------------------------------------------------------------
  73. // Win_Update
  74.  
  75. void Win_Update(
  76.     WindowPtr    theWindow )
  77. {
  78.  
  79.     BeginUpdate( theWindow );
  80.     App_Update( theWindow );
  81.     EndUpdate( theWindow );
  82.  
  83. #ifdef USE_CALIB
  84.     CADrawActiveBorder ();
  85. #endif
  86.  
  87. }
  88.  
  89.  
  90. //---------------------------------------------------------------------------
  91. // Win_ShowSelection - make sure part of the selection is visible in the window.
  92.  
  93. void Win_ShowSelection(
  94.     WindowPtr    theWindow )
  95. {
  96. DocPtr        theDoc;
  97.  
  98.     // if there is no window or it's not ours, get out.
  99.     if (!Win_IsAppWindow( theWindow ))
  100.         return;
  101.  
  102.     theDoc = (DocPtr)GetWRefCon( theWindow );
  103.  
  104. #if 0
  105.     short        topLine;
  106.     short        bottomLine;
  107.     short        aLine;
  108.  
  109.     topLine = ((**theDoc->TEH).viewRect.top - (**theDoc->TEH).destRect.top) / theDoc->lineHeight;
  110.     if (topLine < 0)
  111.         topLine = 0;
  112.     bottomLine = topLine + theDoc->LinesVisible;
  113.     if (bottomLine > (**theDoc->TEH).nLines)
  114.         bottomLine = (**theDoc->TEH).nLines;
  115.  
  116.     if ((**theDoc->TEH).selStart < (**theDoc->TEH).lineStarts[topLine] ||
  117.             (**theDoc->TEH).selStart >= (**theDoc->TEH).lineStarts[bottomLine])
  118.     {
  119.         for (aLine = 0; (**theDoc->TEH).selStart > (**theDoc->TEH).lineStarts[aLine]; aLine++)
  120.             ;
  121.         aLine -= theDoc->LinesVisible / 2;
  122.         if (GetControlValue( theDoc->scrollBar ) != aLine)        // reduce flicker
  123.             SetControlValue( theDoc->scrollBar, aLine );            // set the value.
  124.     }
  125.     AdjustTextToScrollBar( theDoc, bVisible );
  126. #endif
  127. }
  128.  
  129. //----------------------------------------------------------------------
  130. // Adjust the scroll bars and any item rects/rgns in the window.
  131. // Also adjust the ScrollBarMax() if it needs to change.
  132.  
  133. void Win_Adjust(
  134.     WindowPtr    theWindow )
  135. {
  136. DocPtr        theDoc;
  137. short        tempMax;
  138.  
  139.     // if there is no window or it's not ours, get out.
  140.     if (!Win_IsAppWindow( theWindow ))
  141.         return;
  142.  
  143.     theDoc = (DocPtr)GetWRefCon( theWindow );
  144.  
  145.     // adjust the scrollBarRects
  146.     theDoc->hScrollBarRect = theWindow->portRect;
  147.     theDoc->hScrollBarRect.bottom += 1;
  148.     theDoc->hScrollBarRect.left -= 1;
  149.     theDoc->hScrollBarRect.top = theDoc->hScrollBarRect.bottom - SBARWIDTH;
  150.     theDoc->hScrollBarRect.right -= (SBARWIDTH-2);
  151.     HideControl( theDoc->hScrollBar );
  152.     MoveControl( theDoc->hScrollBar, theDoc->hScrollBarRect.left, theDoc->hScrollBarRect.top );
  153.     SizeControl(
  154.         theDoc->hScrollBar,
  155.         theDoc->hScrollBarRect.right - theDoc->hScrollBarRect.left, 
  156.         theDoc->hScrollBarRect.bottom - theDoc->hScrollBarRect.top );
  157.  
  158.     tempMax =
  159.         (theDoc->documentRect.right - theDoc->documentRect.left) - 
  160.         (theDoc->hScrollBarRect.right - theDoc->hScrollBarRect.left);
  161.     if (tempMax < 0)
  162.         tempMax = 0;
  163.     SetControlMaximum( theDoc->hScrollBar, tempMax );
  164.  
  165.     theDoc->vScrollBarRect = theWindow->portRect;
  166.     theDoc->vScrollBarRect.right += 1;
  167.     theDoc->vScrollBarRect.top -= 1;
  168.     theDoc->vScrollBarRect.left = theDoc->vScrollBarRect.right - SBARWIDTH;
  169.     theDoc->vScrollBarRect.bottom -= (SBARWIDTH-2);
  170.     HideControl( theDoc->vScrollBar);
  171.     MoveControl( theDoc->vScrollBar, theDoc->vScrollBarRect.left, theDoc->vScrollBarRect.top );
  172.     SizeControl(
  173.         theDoc->vScrollBar,
  174.         theDoc->vScrollBarRect.right - theDoc->vScrollBarRect.left, 
  175.         theDoc->vScrollBarRect.bottom - theDoc->vScrollBarRect.top );
  176.     tempMax =
  177.         (theDoc->documentRect.bottom - theDoc->documentRect.top) - 
  178.         (theDoc->vScrollBarRect.bottom - theDoc->vScrollBarRect.top);
  179.     if (tempMax < 0)
  180.         tempMax = 0;
  181.     SetControlMaximum( theDoc->vScrollBar, tempMax );
  182.  
  183.     // adjust the contentRect
  184.     theDoc->contentRect = theWindow->portRect;
  185.     theDoc->contentRect.right -= (SBARWIDTH-1);
  186.     theDoc->contentRect.bottom -= (SBARWIDTH-1);
  187.  
  188. #ifdef USE_CALIB
  189.     if (gCALibExists)
  190.     {
  191.         OSErr        theErr;
  192.  
  193.         if (theDoc->partDocRef)
  194.         {
  195.             RgnHandle    rootFrameRgn;
  196.             RgnHandle    tmpRgn;
  197.             CAFrameRef    rootFrameRef;
  198.  
  199.             rootFrameRgn = NewRgn();
  200.             RectRgn (rootFrameRgn, &(theDoc->contentRect));
  201.             rootFrameRef = CAGetRootFrameRef (theDoc->partDocRef);
  202.             if (theErr = CAError())
  203.                 ;        // handle the error
  204.  
  205.             tmpRgn = NewRgn();
  206.             CopyRgn (rootFrameRgn, tmpRgn);
  207.             
  208.             CASetFrameRgn (theDoc->partDocRef, rootFrameRef, rootFrameRgn);
  209.             
  210.             //CAAdjustVisFrame (CAGetVisFrame (theDoc->partDocRef, rootFrameRef), tmpRgn, NULL);
  211.             
  212.         }
  213.     }
  214. #endif
  215.  
  216.     ShowControl( theDoc->hScrollBar );
  217.     ShowControl( theDoc->vScrollBar );
  218.  
  219.     // The scroll bar draws itself, let's avoid the redraw.
  220.     //ValidRect( &theDoc->hScrollBarRect );
  221.     //ValidRect( &theDoc->vScrollBarRect );
  222. }
  223.  
  224. //---------------------------------------------------------------------------
  225. // Win_AdjustStdState - adjust the zoomed out size of the window based on the size
  226. // of the current printer paper.
  227. void Win_AdjustStdState(
  228.     WindowPtr    theWindow )
  229. {
  230. Rect        screenRect;
  231. WStateData    *wsdp;
  232. DocPtr        theDoc;
  233.  
  234.     // if there is no window or it's not ours, get out.
  235.     if (!Win_IsAppWindow( theWindow ))
  236.         return;
  237.  
  238.     theDoc = (DocPtr)GetWRefCon( theWindow );
  239.  
  240.     wsdp = (WStateData*)*((WindowPeek)theWindow)->dataHandle;
  241.     if (wsdp == nil)
  242.         return;
  243.  
  244. #if 0
  245.     // if there is no print handle, leave the stdState as is (size of main screen)
  246. //    if (theDoc->hPrint)
  247. //    {
  248. //        Point    thePt;
  249.  
  250. //        if (EmptyRect(&(**theDoc->hPrint).prInfo.rPage))
  251. //            return;
  252. #endif
  253.  
  254.     wsdp->stdState = theDoc->documentRect;
  255.     wsdp->stdState.bottom += SBARWIDTH-1;    // account for width of scroll bar.
  256.     wsdp->stdState.right += SBARWIDTH-1;    // account for width of scroll bar.
  257.  
  258.     // move the top left to 0,0
  259.     OffsetRect(&wsdp->stdState, -wsdp->stdState.left, -wsdp->stdState.top);
  260.  
  261. #if 0
  262.         // move the top-left stdState to the top-left of the window rect
  263. //        thePt = topLeft(theWindow->portRect);
  264. //        LocalToGlobal(&thePt);
  265. //        OffsetRect(&wsdp->stdState, thePt.h, thePt.v);
  266. #endif
  267.     RectLocalToGlobal( &wsdp->stdState );
  268.  
  269.     // if the bottom right corner is not on the screen, move the window to the 
  270.     // top left of the main screen and try again.
  271.     if (!PtInRgn( botRight(wsdp->stdState), LMGetGrayRgn() ))
  272.     {
  273.         GetMainScreenRect( &screenRect );        /* get rect of main screen */
  274.         OffsetRect(
  275.             &wsdp->stdState,            /* put at top,left of main screen */
  276.             screenRect.left - wsdp->stdState.left + kInitWindowHOffset,
  277.             screenRect.top - wsdp->stdState.top + kInitWindowVOffset);
  278.  
  279.         // make sure the right and bottom are on the screen.
  280.         if (wsdp->stdState.right > screenRect.right)
  281.             wsdp->stdState.right = screenRect.right - 4;
  282.         if (wsdp->stdState.bottom > screenRect.bottom)
  283.             wsdp->stdState.bottom = screenRect.bottom - 4;
  284.     }
  285. //    }
  286. }
  287.  
  288. //----------------------------------------------------------------------
  289. // Scroll the contents of the window and update any exposed areas immediately.
  290.  
  291. void Win_Scroll(
  292.     WindowPtr    theWindow,
  293.     short        hDist,
  294.     short        vDist )
  295. {
  296. DocPtr        theDoc;
  297. RgnHandle    theRgn;
  298.  
  299.     theDoc = (DocPtr)GetWRefCon( theWindow );
  300.  
  301. #ifdef USE_CALIB
  302.     if (gCALibExists)
  303.     {
  304.         Point        offset;
  305.         CAFrameRef    rootFrameRef;
  306.         CATransform    scrollTransform;
  307.         OSErr        theErr;
  308.         CAVisFrame    rootVisFrame;
  309.  
  310.         rootFrameRef = CAGetRootFrameRef( theDoc->partDocRef);
  311.         if (theErr = CAError())
  312.             ;        // handle the error
  313.  
  314. #if 0
  315.         rootVisFrame = CAGetVisFrame (theDoc->partDocRef, rootFrameRef);
  316.         
  317.         CAGetVisFrameTransform(theDoc->partDocRef, rootFrameRef, &scrollTransform);
  318.         offset.h = hDist; offset.v = vDist;
  319.  
  320.         CAMoveTransformBy( &scrollTransform, offset );
  321.         CAAdjustVisFrame (rootVisFrame, NULL, &scrollTransform);
  322. #endif        
  323. #if 1
  324.         CAGetFrameTransform( theDoc->partDocRef, rootFrameRef, &scrollTransform );
  325.         // Offset the root frame of the window being scrolled
  326.         offset.h = hDist; offset.v = vDist;
  327.  
  328.         //CAMoveTransformBy( &scrollTransform, offset );
  329.         offset.h = GetControlValue( theDoc->hScrollBar );
  330.         offset.v = GetControlValue( theDoc->vScrollBar );
  331.         CAMoveTransformTo( &scrollTransform, offset );
  332.  
  333.         CASetFrameTransform( theDoc->partDocRef, rootFrameRef, &scrollTransform );
  334.         if (theErr = CAError())
  335.             ;        // handle the error
  336. #endif
  337.  
  338.     }
  339. #endif
  340.  
  341.     theRgn = NewRgn();
  342.     ScrollRect( &theDoc->contentRect, hDist, vDist, theRgn );
  343.     InvalRgn( theRgn );
  344.     DisposeRgn( theRgn );
  345.  
  346.     Win_Update (theWindow);
  347.     
  348. }
  349.  
  350. //---------------------------------------------------------------------------
  351. // Win_Close - Close the window (after prompting to save changes)
  352.  
  353. OSErr Win_Close(
  354.     WindowPtr    theWindow, Boolean* cancelled )
  355. {
  356. OSErr        theErr = noErr;
  357.  
  358.     if (cancelled) *cancelled = false;
  359.     
  360.     if (Win_IsDAWindow( theWindow ))
  361.     {
  362.         CloseDeskAcc(((WindowPeek) theWindow)->windowKind);
  363.         return theErr;
  364.     }
  365.     else if (Win_IsToolWindow( theWindow ))
  366.     {
  367.         if (theWindow == ToolPalette_GetWindow())
  368.             ToolPalette_Dispose( theWindow );
  369.         else
  370.             DisposeWindow( theWindow );
  371.         return theErr;
  372.     }
  373.     else if (Win_IsAppWindow( theWindow ))
  374.     {
  375.         DocPtr        theDoc;
  376.  
  377.         theDoc = (DocPtr)GetWRefCon( theWindow );
  378.         
  379.         if (Doc_GetDirty( theDoc ))
  380.         {
  381.             short            response;
  382.             Str255            theName;
  383.             Str255            theVerb;
  384.  
  385.             GetWTitle( theWindow, theName );
  386.             GetIndString( theVerb, kStrings, itClosing );    // $$$$$ don't forget itQuitting
  387.             ParamText( theName, theVerb, "\p", "\p" );
  388.             SetCursor( &qd.arrow );
  389.  
  390.             iOKITEM = kSaveButton;
  391.             iCancelITEM = kCancelButton;
  392.  
  393.             App_ForceActivateFrontWindow( false );
  394.             response = Alert( kSaveChangesDialog, gWindowEventFilterUPP );
  395.             App_ForceActivateFrontWindow( true );
  396.  
  397.             if (response == kCancelButton)
  398.             {
  399.                 if (cancelled) *cancelled = true;
  400.                 return theErr;
  401.             } 
  402.             else if (response == kSaveButton)
  403.             {
  404.                 // save the file, creating a new file if needed.
  405.                 theErr = Doc_Save( theDoc );
  406.             }
  407.             else    // kDontSaveButton
  408.             {
  409.             }
  410.         }
  411.  
  412.  
  413. #ifdef USE_CALIB
  414.         if (gCALibExists)
  415.         {
  416.             CARemoveTrackingHandler( gDragTrackingHandlerUPP, theWindow );
  417.             CARemoveReceiveHandler( gDragReceiveHandlerUPP, theWindow );
  418.  
  419.             CAUnregisterWindow( theWindow );
  420.         }
  421. #endif
  422.  
  423.         // get rid of the drag mgr handlers
  424.         // SJF - NOTE: Gotta do this before closing the document or
  425.         //        there's no docRef to work off of.
  426.         if (hasDragMgr)
  427.         {
  428. #ifdef USE_CALIB
  429.             if (!gCALibExists)
  430.             {
  431.                 RemoveTrackingHandler( gDragTrackingHandlerUPP, theWindow );
  432.                 RemoveReceiveHandler( gDragReceiveHandlerUPP, theWindow );
  433.             }
  434. #else
  435.             RemoveTrackingHandler( gDragTrackingHandlerUPP, theWindow );
  436.             RemoveReceiveHandler( gDragReceiveHandlerUPP, theWindow );
  437. #endif
  438.         }
  439.  
  440.         Doc_Close( theDoc );
  441.         App_LogCloseDocument (theDoc);
  442.  
  443.         // close the file (if it exists)
  444.         if (theDoc->fileRefNum != 0)
  445.         {
  446.             theErr = FSClose( theDoc->fileRefNum );
  447.         }
  448.  
  449.         // release the undo memory.
  450.         Doc_DisposeUndo( theDoc );
  451.  
  452.         // get rid of the scroll bars
  453.         DisposeControl( theDoc->hScrollBar );
  454.         DisposeControl( theDoc->vScrollBar );
  455.  
  456.         // finally, get rid of the window itself.
  457.         DisposeWindow( theWindow );
  458.  
  459.         // Make sure the next window gets selected
  460.         App_SetFrontDocWindow( App_GetFrontDocWindow() );
  461.         
  462.         return theErr;    // everything went OK.
  463.     }
  464.  
  465.     return theErr;
  466. }
  467.  
  468. //---------------------------------------------------------------------------
  469. // Win_New - create a B/W or color window.
  470.  
  471. WindowPtr Win_New(
  472.     WindowPtr    behindWindow,
  473.     Rect        *windowRect )
  474. {
  475. WindowPtr    theWindow;
  476. OSErr        theErr;
  477.  
  478.     if (hasColorQD)
  479.         theWindow = GetNewCWindow( kWindowID, nil, behindWindow );
  480.     else
  481.         theWindow = GetNewWindow( kWindowID, nil, behindWindow );
  482.  
  483.     // if there is no window, get out.
  484.     if (theWindow == nil)
  485.         return nil;
  486.  
  487.     SetPort( theWindow );
  488.  
  489.     MoveWindow( theWindow, windowRect->left, windowRect->top, false );
  490.     SizeWindow(
  491.         theWindow, windowRect->right - windowRect->left, 
  492.         windowRect->bottom - windowRect->top, true );
  493.  
  494.     // make sure our window is visible on screen
  495.     Win_CheckWindowPosition( theWindow );
  496.  
  497. #if 0    // Wait until this window has been registered with CALib
  498.         // (if present) to register the DnD handlers...
  499.     // install the drag handlers on the window.
  500.     if (hasDragMgr)
  501.     {
  502.         theErr = InstallTrackingHandler( gDragTrackingHandlerUPP, theWindow, nil );
  503.         if (theErr == noErr)
  504.         {
  505.             theErr = InstallReceiveHandler( gDragReceiveHandlerUPP, theWindow, nil );
  506.             if (theErr != noErr)
  507.                 RemoveTrackingHandler( gDragTrackingHandlerUPP, theWindow );
  508.         }
  509.     }
  510. #endif
  511.  
  512.     return theWindow;
  513. }
  514.  
  515. //---------------------------------------------------------------------------
  516. // Win_IsVisible - create a B/W or color window.
  517.  
  518. Boolean Win_IsVisible(
  519.     WindowPtr    theWindow )
  520. {
  521.     return ((theWindow != nil) && ((WindowPeek)theWindow)->visible);
  522. }
  523.  
  524.  
  525. //---------------------------------------------------------------------------
  526. // Win_DoBackClicks - decide whether a click in a non-front window does
  527. // more than bring the window to the front.
  528.  
  529. Boolean Win_DoBackClicks(
  530.     WindowPtr    theWindow )
  531. {
  532.     return true;
  533. }
  534.  
  535.  
  536. //---------------------------------------------------------------------------
  537. // Win_IsFloater - create a B/W or color window.
  538.  
  539. Boolean Win_IsFloater(
  540.     WindowPtr    theWindow )
  541. {
  542. short        targetWKind;
  543. Boolean        isFloater;
  544.  
  545.     isFloater = false;
  546.     if (theWindow != nil)
  547.     {
  548.         targetWKind = ((WindowPeek)theWindow)->windowKind;
  549.         isFloater = (targetWKind == WINDOWKIND_FLOATING);
  550.     }
  551.  
  552.     return isFloater;
  553. }
  554.  
  555. //---------------------------------------------------------------------------
  556. // Win_SetFloaterKind - create a B/W or color window.
  557.  
  558. short Win_SetFloaterKind(
  559.     WindowPtr    theWindow )
  560. {
  561. short    oldWKind;
  562.  
  563.     if (theWindow == nil)
  564.         return nil;
  565.  
  566.     /* return the old windowKind to the caller, as a convenience */
  567.     oldWKind = ((WindowPeek)theWindow)->windowKind;
  568.  
  569.     /* wedge the windowKind field so that the window can be identified as a floating window */
  570.     ((WindowPeek)theWindow)->windowKind = WINDOWKIND_FLOATING;
  571.  
  572.     return oldWKind;
  573. }
  574.  
  575. //---------------------------------------------------------------------------
  576. // Win_SendActivateEvent - create a B/W or color window.
  577.  
  578. void Win_SendActivateEvent(
  579.     WindowPtr    theWindow,
  580.     Boolean        isActive )
  581. {
  582. EvQElPtr    queueSpot;
  583. OSErr        errStat;
  584.  
  585.     if (!Win_IsVisible( theWindow ))
  586.         return;
  587.  
  588.     errStat = PPostEvent( activateEvt, (long)theWindow, &queueSpot );
  589.     if ((errStat == noErr) && (queueSpot != nil))
  590.     {
  591.         if (isActive)
  592.             queueSpot->evtQModifiers |= activeFlag;
  593.         else
  594.             queueSpot->evtQModifiers &= ~activeFlag;
  595.     }
  596.  
  597. #if defined(Debugging)
  598.     if ((errStat != noErr) || (queueSpot == nil))
  599.         DEBUGSTR( "\pWin_SendActivateEvent: posting event didn't work" );
  600. #endif
  601. }
  602.  
  603. //---------------------------------------------------------------------------
  604. // Win_ShowHide - create a B/W or color window.
  605.  
  606.  
  607. void Win_ShowHide(
  608.     WindowPtr    theWindow,
  609.     Boolean        showIt )
  610. {
  611.     if ((theWindow == nil) || (showIt == Win_IsVisible( theWindow )))
  612.         return;
  613.  
  614.     ShowHide( theWindow, showIt );
  615.     if (showIt && Win_IsFloater( theWindow ))
  616.         HiliteWindow( theWindow, true );
  617. }
  618.  
  619. //---------------------------------------------------------------------------
  620. // Win_DragWindow_Better - similar to DragWindow, but without
  621. //    forcing the target window to the front
  622.  
  623. void Win_DragWindow_Better(
  624.     WindowPtr    theWindow,
  625.     Point        theGlobalPt,
  626.     Rect        *boundsR )
  627. {
  628. CGrafPtr    cDeskPort;
  629. GrafPtr        savedPort;
  630. RgnHandle    wOutline, wArea, savedClip;
  631. Rect        slopR;
  632. Point        localOffset;
  633. long        longDeltaPt;
  634.  
  635.     if ((theWindow == nil) || (boundsR == nil) || EmptyRect( boundsR ))
  636.         return;
  637.  
  638.     GetPort( &savedPort );
  639.     SetPort( theWindow );
  640.     localOffset = theGlobalPt;
  641.     GlobalToLocal( &localOffset );
  642.  
  643.     GetCWMgrPort( &cDeskPort );
  644.     SetPort( (GrafPtr)cDeskPort );
  645.  
  646.     wArea = NewRgn();
  647.     wOutline = NewRgn();
  648.     savedClip = NewRgn();
  649.  
  650.     CopyRgn( ((CWindowRecord*)theWindow)->strucRgn, wArea );
  651.     UnionRgn( wArea, ((CWindowRecord*)theWindow)->contRgn, wArea );
  652.     CopyRgn( wArea, wOutline );
  653.     InsetRgn( wArea, 1, 1 );
  654.     DiffRgn( wOutline, wArea, wOutline );
  655.  
  656.     GetClip( savedClip );
  657.     ClipRect( boundsR );
  658.  
  659.     slopR = *boundsR;
  660.     GlobalToLocal( &theGlobalPt );
  661.     longDeltaPt =
  662.         DragGrayRgn(
  663.             wOutline, theGlobalPt, boundsR, &slopR,
  664.             noConstraint, (DragGrayRgnUPP)nil );
  665.  
  666.     if ((longDeltaPt != 0x80008000) && (longDeltaPt != 0L))
  667.         MoveWindow(
  668.             theWindow,
  669.             (theGlobalPt.h - localOffset.h) + LoWord( longDeltaPt ),
  670.             (theGlobalPt.v - localOffset.v) + HiWord( longDeltaPt ),
  671.             false );
  672.  
  673.     SetClip( savedClip );
  674.  
  675.     DisposeRgn( wArea );
  676.     DisposeRgn( wOutline );
  677.     DisposeRgn( savedClip );
  678.  
  679.     SetPort( savedPort );
  680. }
  681.  
  682. //---------------------------------------------------------------------------
  683. // Win_IsDAWindow - return true if theWindow is a Desk Accessory windows.
  684.  
  685. Boolean Win_IsDAWindow(
  686.     WindowPtr    theWindow )
  687. {
  688.     if (theWindow == nil)
  689.         return false;
  690.     else    // DA windows have negative windowKinds
  691.         return (((WindowPeek)theWindow)->windowKind < 0);
  692. }
  693.  
  694. //---------------------------------------------------------------------------
  695. // Win_IsAppWindow - return true if theWindow is one of our application's windows.
  696.  
  697. Boolean Win_IsAppWindow(
  698.     WindowPtr    theWindow )
  699. {
  700.     if (theWindow == nil)
  701.         return false;
  702.     else if (CAIsPartWindow (theWindow))
  703.         return false;
  704.     else
  705.         return (((WindowPeek)theWindow)->windowKind == userKind);
  706. }
  707.  
  708. //---------------------------------------------------------------------------
  709. // Win_IsToolWindow - return true if theWindow is one of our application's
  710. //     windows, but not a doc window.
  711.  
  712. // DAS: added.
  713. Boolean Win_IsToolWindow(
  714.     WindowPtr    theWindow )
  715. {
  716.     return Win_IsFloater( theWindow );
  717. }
  718.  
  719. //---------------------------------------------------------------------------
  720. // Win_IsDialogWindow - return true if theWindow is a dialog window.
  721.  
  722. Boolean Win_IsDialogWindow(
  723.     WindowPtr    theWindow )
  724. {
  725.     if (theWindow == nil)
  726.         return false;
  727.     else
  728.         return (((WindowPeek)theWindow)->windowKind == dialogKind);
  729. }
  730.  
  731. //----------------------------------------------------------------------
  732. // Win_CheckWindowPosition - makes sure the window title bar is on a visible
  733. // part of the screen and the window is at least as big as the min size
  734. // in the 'wrct' rsrc. If not move it onto the top left of the main screen and
  735. // fix its size. This should be called before the window is made visible.
  736.  
  737. void Win_CheckWindowPosition(
  738.     WindowPtr    theWindow )
  739. {
  740. Rect    boundsRect, screenRect, growRect;
  741. Rect    **theRectHandle;
  742.  
  743.     theRectHandle = (Rect**)GetIndResource( 'wrct', 1 );
  744.     if (theRectHandle != nil)
  745.         growRect = **theRectHandle;
  746.     else
  747.         // set a default growRect.
  748.         SetRect( &growRect, 100, 100, 0x7FFF, 0x7FFF );
  749.  
  750.     boundsRect = theWindow->portRect;        // get bounds rect of window
  751.  
  752.     // check the minimum width
  753.     if (boundsRect.right - boundsRect.left < growRect.left)
  754.         SizeWindow(
  755.             theWindow, growRect.left,
  756.             theWindow->portRect.bottom - theWindow->portRect.top, false );
  757.  
  758.     // check the minimum height
  759.     if (boundsRect.bottom - boundsRect.top < growRect.top)
  760.         SizeWindow(
  761.             theWindow,
  762.             theWindow->portRect.right - theWindow->portRect.left,
  763.             growRect.top, false );
  764.  
  765.     // get bounds rect of window, again
  766.     boundsRect = theWindow->portRect;
  767.  
  768.     RectLocalToGlobal( &boundsRect );
  769.  
  770.     // use the lower 10 pixels of the title bar
  771.     boundsRect.bottom = boundsRect.top;
  772.     boundsRect.top -= 10;
  773.  
  774.     // window is not within gray region
  775.     if (!RectInRgn( &boundsRect, LMGetGrayRgn() ))
  776.     {
  777.         // get rect of main screen
  778.         GetMainScreenRect( &screenRect );
  779.         MoveWindow(
  780.             theWindow, screenRect.left + kInitWindowHOffset,
  781.             screenRect.top + kInitWindowVOffset, false );
  782.     }
  783. }
  784.  
  785. //---------------------------------------------------------------------------
  786. WindowPtr Win_CenterWindow(
  787.     WindowPtr    theWindow )
  788. {
  789. Rect    rWindow, rScreen;
  790.  
  791.     GetMainScreenRect( &rScreen );
  792.     rScreen.top += GetMBarHeight() + 4;
  793.     rWindow = theWindow->portRect;
  794.  
  795.     rCenterRectInRect( &rScreen, &rWindow );
  796.     MoveWindow( theWindow, rWindow.left, rWindow.top, false );
  797.  
  798.     return theWindow;
  799. }
  800.  
  801. //---------------------------------------------------------------------------
  802. WindowPtr Win_CenterWindowOnParentWindow(
  803.     WindowPtr    theWindow )
  804. {
  805. WindowPtr    topWindow;
  806. Rect        rWindow, rParent;
  807.  
  808.     if (theWindow != nil)
  809.     {
  810.         topWindow = App_GetFrontDocWindow();
  811.         if (topWindow == nil)
  812.             GetMainScreenRect( &rParent );
  813.         else
  814.             rParent = topWindow->portRect;
  815.         RectLocalToGlobal( &rParent );
  816.  
  817.         if (theWindow == nil)
  818.             GetMainScreenRect( &rWindow );
  819.         else
  820.             rWindow = theWindow->portRect;
  821.  
  822.         rCenterRectInRect( &rParent, &rWindow );
  823.         MoveWindow( theWindow, rWindow.left, rWindow.top, false );
  824.     }
  825.  
  826.     return theWindow;
  827. }
  828.  
  829. //---------------------------------------------------------------------------
  830. WindowPtr Win_CenterTWindow(
  831.     WindowPtr    theWindow,
  832.     short        iTitleSize )
  833. {
  834. Rect    rWindow, rScreen;
  835.  
  836.     GetMainScreenRect( &rScreen );
  837.     rScreen.top += GetMBarHeight();
  838.     rWindow = theWindow->portRect;
  839.     rWindow.top -= iTitleSize;
  840.  
  841.     rCenterRectInRect( &rScreen, &rWindow );
  842.     MoveWindow( theWindow, rWindow.left, rWindow.top+iTitleSize, false );
  843.  
  844.     return theWindow;
  845. }
  846.  
  847. //---------------------------------------------------------------------------
  848. void Win_CenterShowWindow(
  849.     WindowPtr    theWindow )
  850. {
  851.     if (theWindow != nil)
  852.         ShowWindow( Win_CenterWindow( theWindow ) );
  853. }
  854.  
  855. //---------------------------------------------------------------------------
  856. // handle update and activate events for our doc windows.
  857.  
  858. pascal Boolean bWindowEventFilter(
  859.     DialogPtr        theDialog,
  860.     EventRecord        *theEvent,
  861.     short            *itemHit )
  862. {
  863.     // Note: return true if we know we handled an event, otherwise fall through
  864.     // to the bGenericDialogFilter.
  865.     switch (theEvent->what)
  866.     {
  867.         case updateEvt:
  868.             // try to handle the activate; assume we did not handle it.
  869.             Event_HandleUpdateEvent( theEvent );
  870.             break;
  871.  
  872.         case activateEvt:
  873.             // try to handle the activate; assume we did not handle it.
  874.             Event_HandleActivateEvent( theEvent );
  875.             break;
  876.  
  877.         default:    // for any other event, fall through.
  878.             break;
  879.     }
  880.  
  881.     return bGenericDialogFilter(theDialog, theEvent, itemHit );
  882. }
  883.  
  884. //---------------------------------------------------------------------------
  885. // handle update and activate events for our doc windows in CustomPutFile
  886. // and CustomGetFile. Don't call our bGenericDialogFilter.
  887.  
  888. pascal Boolean bWindowEventYDFilter(
  889.     DialogPtr    theDialog,
  890.     EventRecord    *theEvent,
  891.     short        *itemHit,
  892.     Ptr            yourData )
  893. {
  894.     
  895.     // Note: return true if we know we handled an event, otherwise fall through
  896.     // to the bGenericDialogFilter.
  897.     switch (theEvent->what)
  898.     {
  899.         case updateEvt:
  900.             // try to handle the activate; assume we did not handle it.
  901.             Event_HandleUpdateEvent( theEvent );
  902.             break;
  903.  
  904.         case activateEvt:
  905.             // try to handle the activate; assume we did not handle it.
  906.             Event_HandleActivateEvent( theEvent );
  907.             break;
  908.  
  909.         default:    // for any other event, fall through.
  910.             break;
  911.     }
  912.  
  913.     return false;    // we did not handle the event.
  914.  
  915. //    return bWindowEventFilter(theDialog, theEvent, itemHit );
  916. }
  917.  
  918.  
  919.  
  920.